package com.group5.onlinegame.handler;


import com.github.pagehelper.PageInfo;
import com.group5.onlinegame.dao.IIpDao;
import com.group5.onlinegame.entity.Ip;
import com.group5.onlinegame.service.IIpService;
import com.group5.onlinegame.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class IpMsgHandler {
    @Autowired
    private IIpService ipService;
    //查询并显示用户
    @RequestMapping("ipmsg")
    public ModelAndView UserMsg(@RequestParam(value = "pageNum",defaultValue = "1",required = true) Integer pageNum){
        ModelAndView mv=new ModelAndView("ipmsg");
        PageInfo<Ip> pageInfo=ipService.getIpList(pageNum);
        mv.addObject(pageInfo);
        return mv;
    }
    //全选删除
    @RequestMapping("delallip")
    @ResponseBody
    public String delall(@RequestParam(value = "listid[]") int[] id){
        try {
            ipService.delallip(id);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }

    //全选删除
    @RequestMapping("deloneip")
    @ResponseBody
    public String deloneip(@RequestParam(value = "id") int id){
        try {
            ipService.deloneip(id);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
    //添加ip
    @RequestMapping(value = "addip",method = RequestMethod.POST)
    @ResponseBody
    public String addip(@RequestBody Ip ip, @RequestParam(value = "v")String v){
        int id=Integer.parseInt(v);
        ip.setStatus(id);
        System.out.println(ip);
        try{
            ipService.addip(ip);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
    //查询单个ip
    @RequestMapping("showip")
    @ResponseBody
    public Ip showvip(@RequestParam(value = "id") int id){
        Ip ip=ipService.getIpById(id);
        System.out.println(ip.getDeadtime());
        return ip;
    }
    //添加ip
    @RequestMapping(value = "editip",method = RequestMethod.POST)
    @ResponseBody
    public String editip(@RequestBody Ip ip, @RequestParam(value = "v")String v){
        int id=Integer.parseInt(v);
        ip.setStatus(id);
        System.out.println(ip);
        System.out.println(ip.getId());
        try{
            ipService.editip(ip);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
}
